from collections import OrderedDict

from apps.goods.models import FoodsChannel, FoodsChannelGroup, FoodsCategory


def get_categories():
    """
    专门用来获取商品的分类
    之所以放到goods/utils.py中，而不是views中 是因为除了goods应用中的视图以外还有index视图也需要商品的分类，所以就
    封装到此，这样方便其他应用调用
    :return:
    """
    categories = FoodsChannelGroup.objects.all()
    print(categories)

    # foods_category = FoodsCategory.objects.all()

    return categories


def get_breadcrumb(category):
    """
    获取面包屑导航
    :param category: 商品类别
    :return: 面包屑导航字典
    """
    breadcrumb = dict(
        category_1='',
        category_2='',
        category_3=''
    )
    if category.parent is None:
        # 当前类别为一级类别
        breadcrumb['category_1'] = category
    elif category.subs.count() == 0:
        # 当前类别为三级
        breadcrumb['category_3'] = category
        cat2 = category.parent
        breadcrumb['category_2'] = cat2
        breadcrumb['category_1'] = cat2.parent
    else:
        # 当前类别为二级
        breadcrumb['category_2'] = category
        breadcrumb['category_1'] = category.parent

    return breadcrumb